//
//  collect.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-7-6.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "collect.h"

@implementation collect

+(instancetype)shareData
{static collect *collectData = nil;
    
    if (collectData == nil) {
        collectData = [[collect alloc]init];
    }
    
    return collectData;
    
}
static sqlite3  *Coll =nil;
-(void)openColl
{
    
    if (Coll != nil) {
        return;//如果数据库存在则不往下执行
    }
    //打开数据库函数   将数据库对象与数据库文件关联,并打开数据库
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath  stringByAppendingPathComponent:@"Datacollect.sqlite"];
    int result =  sqlite3_open(sqlitePath.UTF8String, &Coll);
    if (result == SQLITE_OK) {
//        NSLog(@"打开数据库成功");
    }else{
//        NSLog(@"打开数据库失败");
    }
//    NSLog(@"`````````%@",sqlitePath);
    
    
}
-(void)createTable
{
    //写建表的sql语句
    NSString *sql = @"CREATE TABLE IF NOT EXISTS Coll(number INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, displayName TEXT)";
    
    //执行sql语句
    int result = sqlite3_exec(Coll, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
//        NSLog(@"创建成功");
    }else{
        NSLog(@"创建表失败");
        
    }
    
    

    
    
}
//插入
-(void)insertHero:(HeroDetailModel *)hero
{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO Coll(name, displayName)VALUES('%@','%@')",hero.name,hero.displayName];
    int result = sqlite3_exec(Coll, sql.UTF8String, NULL, NULL,nil);
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
    
    
}



//查看
-(NSMutableArray *)selectAll
{
    
    NSString *sql = @"SELECT * FROM Coll";
    //创建数据库跟随指针对象
    sqlite3_stmt *stmt = nil;
    
    //查询准备操作
    //参数3.-1代表不限制sql语句长度
    //主要作用是将数据库对象db,sql语句,数据库跟随指针关联到一起,以便数据库进行查询操作.
    int result =  sqlite3_prepare_v2(Coll, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        NSLog(@"准备成功");
        while(sqlite3_step(stmt) == SQLITE_ROW){
            //取出表中每行中的每个字段的数据
            
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            
            const unsigned char *displayName = sqlite3_column_text(stmt, 2);
            
           HeroDetailModel  *hero = [[HeroDetailModel alloc]init];
            //unsigned 是无符号型   需要强转(const char *)
            hero.displayName = [NSString stringWithUTF8String:(const char *)displayName];
            hero.name = [NSString stringWithUTF8String:(const char *)name];
            [array addObject:hero];
        }
        //释放stmt,清除内存
        sqlite3_finalize(stmt);
           return array;
    }else{
//        NSLog(@"准备失败");
        return nil;
    }
 
}


-(void)deletheroName:(NSString *)name
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM Coll WHERE name = '%@'", name];
    int result = sqlite3_exec(Coll, sql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        //        NSLog(@"删除当地特色收藏数据成功");
    }else {
        //        NSLog(@"删除当地特色收藏数据失败");
    }
}



//删除
-(void)dropTable
{
    NSString *sql = @"DROP TABLE Coll";
    int result = sqlite3_exec(Coll, sql.UTF8String,NULL,NULL ,nil);
    
    if (result == SQLITE_OK) {
//        NSLog(@"删除表成功");
    }else{
        
        NSLog(@"删除表失败");
    }





}






@end
